package sessionbeans;

import java.util.List;
import java.util.LinkedList;
import javax.persistence.*;
import javax.ejb.Stateless;

import entities.*;

import org.jboss.ejb3.annotation.RemoteBinding;
/**
 * Session Bean implementation class NotificationBean
 */
@Stateless
@RemoteBinding(jndiBinding="NotificationBeanRemote")
public class NotificationBean implements NotificationBeanRemote {

	@PersistenceContext
	private EntityManager manager;
	private static final String findAllFriends = "select f from FriendOf as f where f.key.user1 = :user";
	private static final String findAllFriendshipRequests = "select p from PendingFriendship as p where p.key.sender = :sender";
	private static final String findAnsweredHelpRequests = "select h from HelpRequest as h where h.sender = :user and pending = false";
	private static final String findAddedAbilities = "select a from AskForAdding as a where a.key.user = :user and" +
			                                         " a.pending = false and a.answer = true";
	
    /**
     * Default constructor. 
     */
    public NotificationBean() {
        // TODO Auto-generated constructor stub
    }

	@Override
	public List<PendingFriendship> getAcceptedPendingFriendships(String userId) {
		RegisteredUser user = manager.find(RegisteredUser.class, userId);
		Query friendsQuery = manager.createQuery(findAllFriends);
		friendsQuery.setParameter("user", user);
		List<FriendOf> friends = (List<FriendOf>) friendsQuery.getResultList();
		Query requestsQuery = manager.createQuery(findAllFriendshipRequests);
		requestsQuery.setParameter("sender", user);
		List<PendingFriendship> requests = (List<PendingFriendship>) requestsQuery.getResultList();
		List<PendingFriendship> accepted = findAcceptedFriendships(requests, friends);
		removePendingFriendships(accepted);
		return accepted;
	}

	private void removePendingFriendships(List<PendingFriendship> requests) {
		for(PendingFriendship request : requests){
			manager.remove(request);
		}
		
	}

	private List<PendingFriendship> findAcceptedFriendships(List<PendingFriendship> requests, List<FriendOf> friends){
		List<PendingFriendship> accepted = new LinkedList<PendingFriendship>();
		for(PendingFriendship request : requests){
			for(FriendOf friendship : friends){
				String user2 = friendship.getKey().getUser2().getUserId();
				String receiver = request.getKey().getReceiver().getUserId();
				if(user2.equals(receiver)){
					accepted.add(request);
					break;
				}
			}
		}
		return accepted;
	}
	@Override
	public List<HelpRequest> getAnsweredHelpRequests(String userId) {
		RegisteredUser user = manager.find(RegisteredUser.class, userId);
		Query query = manager.createQuery(findAnsweredHelpRequests);
		query.setParameter("user", user);
		List<HelpRequest> requests = (List<HelpRequest>) query.getResultList();
		removeRefusedHelpRequests(requests);
		return requests;
	}
	
	private void removeRefusedHelpRequests(List<HelpRequest> requests){
		for(HelpRequest request : requests){
			if(request.getAnswer() == false){
				manager.remove(request);
			}
		}
	}

	@Override
	public List<AskForAdding> getAcceptedAbilityRequests(String userId) {
		RegisteredUser user = manager.find(RegisteredUser.class, userId);
		Query query = manager.createQuery(findAddedAbilities);
		query.setParameter("user", user);
		List<AskForAdding> requests = (List<AskForAdding>) query.getResultList();
		removeAbilityRequests(requests);
		return requests;
	}
	
	private void removeAbilityRequests(List<AskForAdding> requests){
		for(AskForAdding request : requests){
			manager.remove(request);
		}
	}

}
